home *** CD-ROM | disk | FTP | other *** search
/ Practical Internet 2002 May / Practical Internet May 2002.iso / pc / Software / SiteBuilding / MiniMapperPlus.exe / Favourites to HTML / Favorite.java < prev    next >
Encoding:
Java Source  |  2001-06-15  |  11.4 KB  |  315 lines

  1. /* Convert MSIE Favourites to HTML page */
  2. /* Author: Brian D. Jones               */
  3. /* Date:   10th Jan 97                  */
  4. /* Email:  Brian_Jones@technologist.com */
  5. // History:    18 May 97 Check for "URL=" since not always last line in file.
  6. //          14 Jun 97 Long names which include blanks now supported.
  7. //          26 Jul 97 Explorer 4.0 aware
  8. //            13 Oct 97 Explorer 4.0 Outline added.
  9. //            20 Nov 99 Change Web site URL.
  10. //          26 May 2001 Sort entries.
  11. //          26 May 2001 Make like an Applet for use in HTML Application GUI
  12.  
  13. import java.io.*;
  14. import java.util.*;
  15. import java.applet.*;
  16. import java.awt.*;
  17. import com.ms.security.*;
  18.  
  19. public class Favorite extends Applet {
  20.     private static int level = 0;
  21.  
  22.     private static void ListDir( File Path , PrintStream Out) {
  23.     String Names[] = Path.list();
  24.  
  25.     if (folderFirst) sortF(Names, Path);
  26.     else sort(Names);
  27.  
  28.     if (level == 0)
  29.         Out.println("<div id=\"Out" +level +"\">");
  30.     else
  31.         Out.println("<div id=\"Out" +level +"d\" style=\"display:None\">");
  32.     level = level +1;
  33.     Out.println("<DL>");
  34.     for (int i=0; i < Names.length; i++) {
  35.         try {
  36.         File temp = new File( Path, Names[i] );
  37.         if ( temp.isDirectory() ) {
  38.             Out.println( "<DT><div id=\"Out" +level +"\" class=\"Outline\" style=\"cursor: hand\"><IMG SRC=\"" +iconPath +"urlfldr.gif\" WIDTH=16 HEIGHT=16 id=\"Out" +level +"\" class=\"Outline\" hspace=4> "+ temp.getName() +"</div><DD>" );
  39.  
  40.             ListDir ( temp, Out );
  41.             Out.println( "</DT>" );
  42.         }
  43.         else if (temp.getName().endsWith(".url")) {
  44.             try {
  45.             String BaseName = temp.getName();
  46.             String sURL = null,
  47.                 sDesc = null,
  48.                 sAuthor = null,
  49.                 sWhatsNew = null,
  50.                 sAccessed = null, sModified = null;
  51.             DataInputStream file = new DataInputStream( new FileInputStream( temp ));
  52.             String Line= null;
  53.             Date dateModified = null,
  54.                 dateAccessed = null;
  55.  
  56.             while ( file.available() > 0 ) {
  57.                 Line = file.readLine();
  58. //                if (Line.startsWith("URL=")) break; // Not always last line.
  59.                 if (Line.startsWith("URL="))
  60.                     sURL = Line.substring( Line.indexOf( "=" )+1);
  61. //                These items appeared in the Beta, but don't seem to be in the final version.
  62.                 if (Line.startsWith("Desc="))
  63.                     sDesc = Line.substring( Line.indexOf( "=" )+1);
  64.                 if (Line.startsWith("Author="))
  65.                     sAuthor = Line.substring( Line.indexOf( "=" )+1);
  66.                 if (Line.startsWith("WhatsNew="))
  67.                     sWhatsNew = Line.substring( Line.indexOf( "=" )+1);
  68.                 if (Line.startsWith("Accessed="))
  69.                     sAccessed = Line.substring( Line.indexOf( "=" )+1);
  70.                 if (Line.startsWith("Modified="))
  71.                     sModified = Line.substring( Line.indexOf( "=" )+1);
  72.             }
  73.             file.close();
  74. //            Line = Line.substring( Line.indexOf( "=" )+1);
  75.             BaseName = BaseName.substring( 0, BaseName.lastIndexOf( "." ));
  76.             Out.print( "<DT>");
  77.             try {
  78.                 if (sModified != null) {
  79.                     String tmp = null;
  80.                     StringTokenizer token = new StringTokenizer( sModified, ",", false );
  81.                     tmp = token.nextToken().substring(2);
  82.                     dateModified = new Date( Long.parseLong(tmp,16));
  83. //                    Out.println(" Modified: " +dateModified.toLocaleString());
  84.                 }
  85.                 if (sAccessed != null) {
  86.                     String tmp = null;
  87.                     StringTokenizer token = new StringTokenizer( sAccessed, ",", false );
  88.                     tmp = token.nextToken().substring(2);
  89.                     dateAccessed = new Date( Long.parseLong(tmp,16));
  90. //                    Out.println(" Modified: " +dateAccessed.toLocaleString());
  91.                 }
  92.             }
  93.             catch ( Exception e ) { } // Out.println( "<!-- " +e +" -->");}
  94.             Out.print( "<A HREF=\"" +sURL +"\">");
  95.             if ( dateAccessed != null
  96.                 && dateModified != null
  97.                 && dateAccessed.after(dateModified))
  98.                 Out.print( "<IMG SRC=\"" + iconPath + "urlchg.gif\" WIDTH=16 HEIGHT=16 border=0 hspace=4>");
  99.             else
  100.                 Out.print( "<IMG SRC=\"" + iconPath + "url.gif\" WIDTH=16 HEIGHT=16 Border=0 hspace=4>");
  101.             Out.println( BaseName +"</A>");
  102.             if (sWhatsNew != null)
  103.                 Out.println("<DD>What's new: " +sWhatsNew +"</DD>");
  104.             else if (sDesc != null )
  105.                 Out.println("<DD>" +sDesc +"</DD>");
  106.             }
  107.             catch ( Exception e ) { ;
  108.             }
  109.         }
  110.         else if (temp.getName().equalsIgnoreCase("desktop.ini")) {
  111.             try {
  112.             String BaseName = Path.getName();
  113.             String sURL = null,
  114.                 sLogo = "" + iconPath +"url.gif",
  115.                 sWideLogo = null;
  116.             DataInputStream file = new DataInputStream( new FileInputStream( temp ));
  117.             String Line= null;
  118.  
  119.             while ( file.available() > 0 ) {
  120.                 Line = file.readLine();
  121. //                if (Line.startsWith("URL=")) break; // Not always last line.
  122.                 if (Line.startsWith("CDFURL="))
  123.                     sURL = Line.substring( Line.indexOf( "=" )+1);
  124.                 if (Line.startsWith("Logo="))
  125.                     sLogo = Line.substring( Line.indexOf( "=" )+1);
  126.                 if (Line.startsWith("WideLogo="))
  127.                     sWideLogo = Line.substring( Line.indexOf( "=" )+1);
  128.             }
  129.             file.close();
  130.             if (sURL != null) {
  131.                 Out.print( "<A HREF=\"" +sURL +"\">");
  132.                 if (sWideLogo != null)
  133.                     Out.print( "<IMG SRC=\"" +sWideLogo +"\"></A>");
  134.                 else
  135.                     Out.print( "<IMG SRC=\"" +sLogo +"\"></A>");
  136.             }
  137.             }
  138.             catch ( Exception e ) { ;
  139.             }
  140.         }
  141.         }
  142.         catch ( Exception e ) { continue;
  143.         }
  144.     }
  145.     Out.println("<P></DL>");
  146.     Out.println("</div>");
  147.     }
  148.  
  149.     public static void DoIt( String Path, PrintStream Out ) {
  150.  
  151.     File Root = null;
  152.  
  153.     try {
  154.         Root = new File( Path );
  155.     }
  156.     catch  ( Exception e ) {
  157.         Out.println("<HTML><HEAD><TITLE>404 Not Given</TITLE></HEAD>");
  158.         Out.println("<BODY><H1>404 Not Given</H1>No path was requested.<P></BODY></HTML>");
  159.         return;
  160.     }
  161.     if ( !Root.exists()
  162.     || !Root.isDirectory()) {
  163.         Out.println("<HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD>");
  164.         Out.println("<BODY><H1>404 Not Found</H1>The requested path <EM>" +Path +"</EM> was not found on this server<P></BODY></HTML>");
  165.         return;
  166.     }
  167.      Out.print( "<HTML><HEAD>");
  168.     Out.println( "<TITLE>" +Root.getName() +"</TITLE>");
  169.  
  170.     Out.println("<script language=\"JavaScript\">");
  171.     Out.println("<!--");
  172.     Out.println("function clickHandler() {");
  173.     Out.println("  var targetId, srcElement, targetElement;");
  174.     Out.println("  if (window.event) srcElement = window.event.srcElement;");
  175.     Out.println("  else srcElement = clickHandler.arguments[0].target;");
  176.     Out.println("  if (srcElement.className == \"Outline\") {");
  177.     Out.println("     targetId = srcElement.id + \"d\";");
  178.     Out.println("     if (document.getElementById) targetElement = document.getElementById(targetId);");
  179.     Out.println("     else if (document.all) targetElement = document.all(targetId);");
  180.     Out.println("     else return;");
  181.     Out.println("     if (targetElement.style.display == \"none\") {");
  182.     Out.println("        targetElement.style.display = \"\";");
  183.     Out.println("        document.images(srcElement.id).src = \"" + iconPath + "urlfldro.gif\";");
  184.     Out.println("        setCookie( targetId, \"show\" );");
  185.     Out.println("     } else {");
  186.     Out.println("        targetElement.style.display = \"none\";");
  187.     Out.println("        document.images(srcElement.id).src = \"" + iconPath + "urlfldr.gif\";");
  188.     Out.println("        setCookie( targetId, \"none\" );");
  189.     Out.println("     }");
  190.     Out.println("  }");
  191.     Out.println("}");
  192.     Out.println("");
  193.     Out.println("function setCookie(name, value) {");
  194.     Out.println("  document.cookie = name + \"=\" + escape(value);");
  195.     Out.println("}");
  196.     Out.println("function readtree() {");
  197.     Out.println("  var dc = document.cookie;");
  198.     Out.println("  if (dc.length > 0) {");
  199.     Out.println("    ca = dc.split(\";\");");
  200.     Out.println("    for ( c in ca ) {");
  201.     Out.println("      mid = ca[c].indexOf(\"=\");");
  202.     Out.println("      if (ca[c].charAt(0) == \" \") targetId = ca[c].substring(1,mid)");
  203.     Out.println("      else  targetId = ca[c].substring(0,mid)");
  204.     Out.println("      if ( targetId.substr(0,3) != \"Out\") continue;");
  205.     Out.println("      val = ca[c].substr(mid+1,4);");
  206.     Out.println("      if (document.getElementById) targetElement = document.getElementById(targetId);");
  207.     Out.println("      else if (document.all) targetElement = document.all(targetId);");
  208.     Out.println("      else return;");
  209.     Out.println("      if (val == \"show\") {");
  210.     Out.println("        targetElement.style.display = \"\";");
  211.     Out.println("        document.images(targetId.substr(0,targetId.length-1)).src = \"" + iconPath + "urlfldro.gif\";");
  212.     Out.println("      }");
  213.     Out.println("    }");
  214.     Out.println("  }");
  215.     Out.println("}");
  216.     Out.println("document.onclick = clickHandler;");
  217.     Out.println("//-->");
  218.     Out.println("</script>");
  219.     Out.println( "<style type=\"text/css\">");
  220.     Out.println(" body { font-family: \"verdana,arial,helvetica\"; font-size:10pt;}");
  221.     Out.println(" .head { font-size:14pt; font-weight: bold;}");
  222.     Out.println(" .OutLine { font-weight: bold; }");
  223.     Out.println("</style>");
  224.     Out.println( "</HEAD>" );
  225.  
  226.     Out.print("<body onload=\"readtree();\">");
  227.     Out.println( "<div class=\"head\"><IMG SRC=\"" + iconPath + "urlfldr.gif\" WIDTH=32 HEIGHT=32>" +Root.getName() +"</div><HR>");
  228.  
  229.         ListDir( Root, Out );
  230.  
  231.     Out.println( "<HR>" );
  232.     Out.println( "Created: " +new Date().toLocaleString() );
  233.     Out.println( "using Java application created by <A HREF=\"http://www.jonessoft.free-online.co.uk\">Brian D. Jones</A>" );
  234.     Out.println( "<A HREF=\"mailto:Brian_Jones@technologist.com\">Email: Brian_Jones@technologist.com</A>" );
  235.     Out.println( "</BODY>" );
  236.  
  237.     }
  238.  
  239.     private static String cmdLine( String args[]) {
  240.         String returnString = args[0];
  241.         for ( int i=1; i< args.length; i++) returnString = returnString +" " +args[i];
  242.         return returnString;
  243.     }
  244.  
  245.     private static boolean folderFirst = false;
  246.     private static String iconPath = "icons/";
  247.     private static void sort( String toSort[]) {
  248.         if (toSort.length<2) return;
  249.         int i,j;
  250.         for (i=0; i < toSort.length ; i++)
  251.             for (j=i+1; j < toSort.length ; j++) 
  252.                 if (toSort[j].compareTo(toSort[i])< 0) {
  253.                     String temp = toSort[i];
  254.                     toSort[i] = toSort[j];
  255.                     toSort[j] = temp;
  256.                 }
  257.     }
  258.  
  259.     private static void sortF( String toSort[], File Path) {
  260.         if (toSort.length<2) return;
  261.         int i,j;
  262.         for (i=0; i < toSort.length ; i++) {
  263.             File fileA = new File( Path, toSort[i] );
  264.             for (j=i+1; j < toSort.length ; j++) {
  265.                 File fileB = new File( Path, toSort[j]);
  266.                 if ( (fileB.isDirectory() && fileA.isFile() )
  267.                 || ( ((fileA.isDirectory() && fileB.isDirectory() )
  268.                 ||   (fileA.isFile() && fileB.isFile()) )
  269.                 && (toSort[j].compareTo(toSort[i])< 0) ) ) {
  270.                     String temp = toSort[i];
  271.                     toSort[i] = toSort[j];
  272.                     toSort[j] = temp;
  273.                     fileA = fileB;
  274.                 }
  275.             }
  276.         }
  277.     }
  278.  
  279.     public void init()
  280.     {
  281.         setForeground( Color.black  );
  282.         setBackground( Color.green  );
  283.     }
  284.  
  285.     public void setOrder( boolean order ) {
  286.         folderFirst = order;
  287.     }
  288.     public void setPath( String Path ) { 
  289.         if (Path==null) iconPath = "";
  290.         else iconPath = Path.replace('\\','/');
  291.     }
  292.     public void save(String folder, String toFile) {
  293.     PolicyEngine.assertPermission(PermissionID.SYSTEM);
  294.         FileOutputStream saveFile = null;
  295.         level = 0;
  296.         try {
  297.             saveFile = new FileOutputStream (toFile) ;
  298.         DoIt( folder, new PrintStream( saveFile ));
  299.         saveFile.close();
  300.         } catch (Exception e) {}
  301.     }
  302.     public static void main (String args[] ) {
  303.     System.out.println("Content-type: text/html");
  304.     System.out.println("");
  305.     if (args.length < 1) {
  306.         System.out.println("<HTML><HEAD><TITLE>404 Not Given</TITLE></HEAD>");
  307.         System.out.println("<BODY><H1>404 Not Given</H1>No path was requested.<P></BODY></HTML>");
  308.         return;
  309.     }
  310.  
  311.     DoIt( cmdLine( args ), System.out );
  312.     }
  313. }
  314.  
  315.